﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZEFIT
{
    public partial class CategoriesForm : Form
    {
        public MainForm mainForm = null;
        public Controller controller = null;
        BindingList<WorkingCategory> categoryList = null;

        public CategoriesForm(MainForm mainForm)
        {
            InitializeComponent();
            this.mainForm = mainForm;
            controller = new Controller(new MYSQLAdapterWorkingCategory());
            dataLoad();
        }

        public void dataLoad()
        {
            categoryList = null;
            categoryList = new BindingList<WorkingCategory>();
            foreach (List<object> tempList in controller.Select())
            {
                WorkingCategory temp = new WorkingCategory();
                temp.Id = (int)tempList[0];
                temp.Name = (String)tempList[1];
                categoryList.Add(temp);
            }
            dataGridView1.DataSource = categoryList;
        }

        private void CategoriesForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.mainForm.openForm();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            WorkingCategory category = (WorkingCategory)dataGridView1.SelectedRows[0].DataBoundItem;
            if (category != null)
            {
                NewCategoryForm newCategory = new NewCategoryForm(this, category);
                newCategory.ShowDialog();
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            WorkingCategory category = (WorkingCategory)dataGridView1.SelectedRows[0].DataBoundItem;
            if (category != null)
            {
                controller.Delete(category);
                dataLoad();
            }
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            NewCategoryForm newCategory = new NewCategoryForm(this);
            newCategory.ShowDialog();
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            this.mainForm.openForm();
            this.Close();
        }
    }
}
